package jsyf.oa.service.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.report.RegionReportDao;
import jsyf.oa.facade.report.RegionReportFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/2/7 0007.
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class RegionReportService implements RegionReportFacade{
    private static String one = "1";
    private static String two = "2";
    @Autowired
    private RegionReportDao dao;
    @Override
    public JSONObject getByUserId(String jsonData) {
        JSONObject result = new JSONObject();
        JSONObject todays=new JSONObject();
        JSON months= new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = jsonParams.getString("companyId");
        String type = jsonParams.getString("type");
        Date dayCurrent = dao.getCurrentTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = sdf.format(dayCurrent);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dayCurrent);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        if (one.equals(type)){
            if (StringUtils.isNotBlank(companyId)){
                todays=dao.getByAllCompanyId(dateTime);
            }
        }
        else if (two.equals(type)){
            if (StringUtils.isNotBlank(companyId)){
                todays=dao.getByCompanyId(companyId,dateTime,yesterday);
            }
        }else {
            if (StringUtils.isNotBlank(userId)){
                todays=dao.getByUserId(userId,dateTime,yesterday);
            }
        }
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM");
        Date currentTime = dao.getCurrentTime();
        String month=sdfDay.format(currentTime);
        if (one.equals(type)){
            if (StringUtils.isNotBlank(companyId)){
                months=dao.getByAllCompanyId(month);
            }
        }
        else if (two.equals(type)){
            if (StringUtils.isNotBlank(companyId)){
                months=dao.getByCompanyId(companyId,month,"");
            }
        }else {
            if (StringUtils.isNotBlank(userId)){
                months=dao.getByUserId(userId,month,"");
            }
        }
        result.put("today", todays);
        result.put("month", months);
        return result;
    }
    @Override
    public List<JSONObject> getCompanyByManage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        return dao.getCompanyByManage(dateTime);
    }
    @Override
    public List<JSONObject> getRegionByManage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        return dao.getRegionByManage(dateTime,companyId);
    }

    @Override
    public List<JSONObject> getCustomerByManage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String entourage = jsonParams.getString("entourage");
        String dateTime = jsonParams.getString("dateTime");
        String isApp = jsonParams.getString("isApp");
        String yesterday = getYesterDay();
        if (dateTime.length() == 10) {
            dateTime= getIsApp(isApp, dateTime, yesterday);
        }
        List<JSONObject>customer=dao.getCustomerByManage(dateTime,userId,entourage);
        //根据入职时间计算出入职天数
        List<JSONObject> listSendData = getEntryDate(customer);
        return listSendData;
    }

    public List<JSONObject> getEntryDate(List<JSONObject> listUser) {
        int dayNumber = 0;
        List<JSONObject> listSendData = new ArrayList<>();
        for (int i = 0; i < listUser.size(); i++) {
            JSONObject users = listUser.get(i);
            String entryDate = users.getString("entryDate");
            if (!StringUtils.isBlank(entryDate) && !"".equals(entryDate)) {
                    try {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date startDate = simpleDateFormat.parse(entryDate);
                        Date nowDate = dao.nowDate();
                        Long aLong = nowDate.getTime() - startDate.getTime();
                        dayNumber = (int) (aLong / (1000 * 60 * 60 * 24));
                        users.put("dayNumber", dayNumber);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
             else {
                users.put("dayNumber", 0);
            }
            listSendData.add(users);
        }
        return listSendData;
    }

    private String getIsApp(String isApp, String dateTime, String yesterday) {
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isNotBlank(isApp) && isApp.equals("1")) {
            boolean flag = DateUtils.isInDate(calendar.getTime(), "00:00:00", "08:00:00");
            if (flag) {
                dateTime = yesterday;
                return dateTime;
            }else {
                return dateTime;
            }
        }else {
            boolean flag = DateUtils.isInDate(calendar.getTime(), "00:00:00", "12:00:00");
            if (flag) {
                dateTime = yesterday;
                return dateTime;
            }
            return dateTime;
        }
    }

    public String getYesterDay() {
        //通过传过来的时间算出前一天
        Date dayCurrent = dao.getCurrentTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dayCurrent);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        dayCurrent = calendar.getTime();
        return sdf.format(dayCurrent);
    }
}
